package com.example.lishop.controller

import com.example.lishop.base.MyResponse
import com.example.lishop.entity.BProductEntity
import com.example.lishop.entity.bean.BaseListBean
import com.example.lishop.service.impl.ProductServiceImpl
import com.example.lishop.util.SnowflakeUtil
import io.swagger.annotations.Api
import io.swagger.annotations.ApiOperation
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.*

/**
 * Created on 2021/3/14 18:28
 * @author shuguo
 * 商品
 */
@Api(value = "/Product", tags = arrayOf("商品接口"))
@RestController
@RequestMapping("/product")
class ProductCtl {

    @Autowired
    lateinit var service: ProductServiceImpl

    @ApiOperation(value = "添加商品")
    @PostMapping(value = ["/add"])
    fun add(@RequestBody bean: BProductEntity) : MyResponse<BProductEntity> {
        bean.productId = ""+ SnowflakeUtil.instance.nextId(2)
        return MyResponse(200, "添加成功", service.add(bean))
    }

    @ApiOperation(value = "修改商品")
    @PostMapping(value = ["/update"])
    fun update(@RequestBody @Validated bean:BProductEntity) : MyResponse<BProductEntity> {
        service.update(bean)
        return MyResponse(200, "修改成功", detail(bean).data)
    }

    @ApiOperation(value = "商品列表")
    @PostMapping(value = ["/list"])
    fun list(@RequestBody @Validated bean: BaseListBean) : MyResponse<Page<BProductEntity>> {
        return MyResponse(200, "请求列表成功", service.list(bean.page, bean.size))
    }

    @ApiOperation(value = "删除商品")
    @PostMapping(value = ["/del"])
    fun del(@RequestBody @Validated bean:BProductEntity) : MyResponse<Unit> {
        return MyResponse(200, "删除成功", service.del(bean))
    }

    @ApiOperation(value = "查询商品详情")
    @PostMapping(value = ["/detail"])
    fun detail(@RequestBody @Validated bean:BProductEntity) : MyResponse<BProductEntity> {
        return MyResponse(200, "查找成功", service.detail(bean))
    }

    @ApiOperation(value = "根据businessId查询商品列表")
    @PostMapping(value = ["/listByBusinessId"])
    fun listByBusinessId(@RequestParam businessId: String) : MyResponse<List<BProductEntity>> {
        return MyResponse(200, "请求列表成功", service.listByBusinessId(businessId))
    }
}